#ifndef __M_SINK_H__
#define __M_SINK_H__
#include<memory>
#include"util.hpp"
#include<cassert>
#include<fstream>
#include<sstream>


namespace bitlog{
    class LogSink{
        public:
          using ptr=std::shared_ptr<LogSink>;
          LogSink(){}
          virtual ~LogSink(){}
          virtual void log(const char* data,size_t len)=0;
    };
    class StdoutSink:public LogSink{
        public:
          void log(const char* data,size_t len)
          {
            std::cout.write(data,len);//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
          }
    };
    class FileSink:public LogSink{
        public:
        FileSink(const std::string &pathname):_pathname(pathname)
        {
           util::File::createDirectory(util::File::path(pathname));//创建目录@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
           _ofs.open(_pathname,std::ios::binary|std::ios::app);//创建文件@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
           assert(_ofs.is_open());
        }
        void log(const char* data,size_t len)
        {
            _ofs.write(data,len);
            assert(_ofs.good());//判断当前的这个操作句柄是否还是正常的@@@@@@@@@@@@@@@@@@@@@
        }
        private:
         std::string _pathname;
         std::ofstream _ofs;
    };
    class RollBySizeSink:public LogSink{
      public:
        RollBySizeSink(const std::string &basename,size_t max_size):
        _basename(basename),_max_fsize(max_size),_cur_fsize(0),_name_count(0)
        {
            std::string pathname=createNewFile();
            util::File::createDirectory(util::File::path(pathname));
            _ofs.open(pathname,std::ios::binary|std::ios::app);
            assert(_ofs.is_open());
        }
        void log(const char *data,size_t len){
            if(_cur_fsize>=_max_fsize)
            {
                _ofs.close();//关闭原来打开的文件
                std::string pathname=createNewFile();
                _ofs.open(pathname,std::ios::binary|std::ios::app);
                assert(_ofs.is_open());
                _cur_fsize=0;

            }
            _ofs.write(data,len);
            assert(_ofs.good());
            _cur_fsize+=len;
        }
      private:
        std::string createNewFile(){
            time_t t=util::Date::now();
            struct tm lt;
            localtime_r(&t,&lt);
            std::stringstream filename;
            filename<<_basename;
            filename<<lt.tm_year+1900;
            filename<<lt.tm_mon+1;
            filename<<lt.tm_mday;
            filename<<lt.tm_hour;
            filename<<lt.tm_min;
            filename<<lt.tm_sec;
            filename<<"-";
            filename<<_name_count++;
            filename<<".log";
            return filename.str();//@@@@@@@@@@@@转化成string类型
        }
      private:
        size_t _name_count;
        std::string _basename;
        std::ofstream _ofs;
        size_t _max_fsize;
        size_t _cur_fsize;

    };


    class SinkFactory{
        public:
            template<typename SinkType,typename ...Args>
            static LogSink::ptr create(Args &&...args){
               return std::make_shared<SinkType>(std::forward<Args>(args)...);//...的目的是为了展开
         }
    };
}

#endif